﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Comment;
using BLL.Comment.Impl;
using BLL.Users;
using BLL.Users.Impl;
using Model;
using Utils;
using System.Text;

public partial class Page_Expert_ExpertOfDetail : System.Web.UI.Page
{
    string expertId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
       expertId = Request["expertId"].ToString();
       expertDetailDataBind();
       expertCommentDataBind();
        //专家点击量加1
       addClickAmount(expertId);
       UIControl();
    }
    private void UIControl()
    {
        if (WebUtil.GetIdentifier(Session["userId"]) == "")
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script>");
            script.Append("document.getElementById('PostExpertAComment').style.display='none';");
            script.Append("</script>");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
        }
        else
        {
            string userId = WebUtil.GetIdentifier(Session["userId"]);
            UserBO userBO = new UserBOImpl();
            User user = userBO.Find(userId);
            if (user.UserType == "Expert")
            {
                StringBuilder script = new StringBuilder();
                script.Append("<script>");
                script.Append("document.getElementById('PostExpertAComment').style.display='none';");
                script.Append("</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
            }
        }

    }
    protected void btnExpertCommentSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string expertCommentDetail = txtExpertComment.Text.Trim();

        ExpertComment expertComment = new ExpertComment() 
        {
            ExpertCommentDetail=expertCommentDetail,
            ExpertId = expertId,
            EnterpriseId=Session["userId"].ToString()
        };

        ExpertCommentBO expertCommentBO = new ExpertCommentBOImpl();

        bool result = expertCommentBO.Add(expertComment);

        if (result)
        {
            Response.Redirect(Request.Url.ToString()); 
        }
        else
        {
            Response.Write("<script>alert('Reply Fail!')</script>");
        }

    }
    protected void LBPoster_Click(object sender, EventArgs e)
    {
        LinkButton bt=(LinkButton)sender;
        string enterpriseId=bt.CommandArgument.ToString();
        Response.Redirect("../Enterprise/EnterpriseDetails.aspx?enterpriseId=" + enterpriseId);

    }
    protected void btnExpertInfoBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script language=javascript>history.go(-2);</script>");
    }
    //显示专家详细信息数据源的绑定
    private void expertDetailDataBind()
    {
        ExpertBO expertBO=new ExpertBOImpl();
        List<Expert> expertDetail = expertBO.GetEntityList("ExpertId",expertId,"ExpertClickAmount");
        dvExpertInfo.DataSource = expertDetail;
        dvExpertInfo.DataBind();
    }
    //显示专家评论信息数据源的绑定
    private void expertCommentDataBind()
    {
        ExpertCommentBO expertCommentBO = new ExpertCommentBOImpl();
        List<ExpertComment> expertComment = expertCommentBO.GetEntityList("ExpertId", expertId,"ExpertCommentDate");
        gvExpertComment.DataSource = expertComment;
        gvExpertComment.DataBind();
    }
    /// <summary>
    /// 对该专家的点击量加1
    /// </summary>
    private void addClickAmount(string expertId)
    {

        ExpertBO expertBO = new ExpertBOImpl();
        Expert expert = expertBO.Find(expertId);
        expert.ExpertClickAmount++;
        expertBO.Update(expert);

    }
}